#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gprs.h"
#include "ddebug.h"

/*
	int fd;
	fd = open("/dev/myadc",O_RDWR|O_NOCTTY|O_NDELAY);//读写；不允许进程管理串口；
											//	非阻塞	
*/

static int fd;
static char *phone_number;
int getMessage(char *str,char *message)
{
	char number[11];
	char data[5500];

	char *p,*q;
	int n;
	int dataLength;
	int length = strlen(str);
	printf("*****************str = %s*************\n",str);
	printf("length = %d\n",length);

	n=0;
	p=str;
	while (1)
	{
		q=strchr(p,'"');
		if (NULL==q) break;
		p=q+1;
		n++;

		if (3==n) 
		{
			strncpy(number,q+4,11);
        	}

		if (8==n) 
		{
			dataLength = length -(q-str) - 1;
			printf("dataLength = %d\n",dataLength);
			strncpy(data,q+1,dataLength);
			break;
        	}
    	}
	
	strncpy(message,number,12);
	strcat(message,data); 
	printf("%s\n",message);
	return 0;
}

int check_fd_gprs()
{
	if(fd < 0)
	{
		DDEBUG(P_URGENCY,"gprs fd check bad.\n");
		return fd;
	}
	return 1;
}


void  serial_init(char *dev_name)
{
	phone_number = (char *)malloc(20);
	bzero(phone_number,20);
	DDEBUG(P_NORMAL,"get dev_name:%s\n",dev_name);
	fd =open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY);
	if(fd < 0)
	{
		DDEBUG(P_URGENCY,"gprs fd check bad.\n");
		return;
	}

	struct termios options;
	tcgetattr(fd, &options);
	options.c_cflag |= ( CLOCAL | CREAD );/*input mode flag:ignore modem 
										  control lines; Enable receiver */
	options.c_cflag &= ~CSIZE;
	options.c_cflag &= ~CRTSCTS;
	options.c_cflag |= CS8;
	options.c_cflag &= ~CSTOPB; //停止位
	options.c_iflag |= IGNPAR;	// 忽略校验错误
	options.c_oflag = 0;// 无输出模式
	options.c_lflag = 0; //本地模式禁用
	cfsetispeed(&options, B115200);
	cfsetospeed(&options, B115200);
	tcsetattr(fd,TCSANOW,&options);
}

int set_phone_number(unsigned char *number)
{
	strncpy(phone_number,(char *)number,20);
	return 0;
}

void swap(char number[],char swap[])
{
	char *ch1 = "86";
	char tmp[16];
	int i;
	memset(swap,0,32);
	memset(tmp,0,16);
	strcpy(swap,number);
	strcat(swap,"f");
	strcat(ch1,swap);
	strcpy(swap,ch1);
	for(i = 0;i <= strlen(swap) - 1;i += 2){
		tmp[i + 1] = swap[i];
		tmp[i] = swap[i + 1];
	}
	strcpy(swap,tmp);
}
/*
 *cmgf 选择短信发送格式
 *
 *cmgs 发送短信
 */

int send(char *cmgf,char *cmgs,char *message)//发送函数，用于AT指令的发送
{
	if(fd < 0)
	{
		DDEBUG(P_URGENCY,"in the send, gprs fd check bad.\n");
		return fd;
	}

	int nread,nwrite;
	char buff[128];
	char reply[128];
//	char *cscs = "GSM";//文本模式发送


	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT\r");
	nwrite = write(fd,buff,strlen(buff));
	printf("1_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("1_nread = %d, %s\n", nread, reply);

//////////////////////////////////////////////////////////
/*发送方式*/
	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT+CMGF=");
	strcat(buff,cmgf);
	strcat(buff,"\r");
	nwrite = write(fd,buff,strlen(buff));
	printf("2_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("2_nread = %d, %s\n", nread, reply);

/********************************************/
	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT+CSCS=");
	strcat(buff,"\"GSM\"");
	strcat(buff,"\r");
	printf("buf = %s\n",buff);
	nwrite = write(fd,buff,strlen(buff));
	printf("3_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("3_nread = %d, %s\n", nread, reply);
/********************************************/

	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT+CMGS=");
	strcat(buff,cmgs);
	strcat(buff,"\r");
	nwrite = write(fd,buff,strlen(buff));
	printf("4_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("4_nread = %d, %s\n", nread, reply);
/**/
	memset(buff,0,sizeof(buff));
	strcpy(buff,message);
	nwrite = write(fd,buff,strlen(buff));
	printf("5_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("5_nread = %d, %s\n", nread, reply);

	printf("call send over\n");	
	return 0;
}
int send_en_message(char *info,char *number)//发送处理函数，处理字符串
{
	if(fd < 0)
	{
		DDEBUG(P_URGENCY,"in the send message, gprs fd check bad.\n");
		return fd;
	}
	char *cmgf = "1";//文本模式发送
	//int conter = 0;
	char cmgs[16] = {'\0'};
	char buf[128] = {0};
	if(number == NULL)
	{
		number = phone_number;
	}
		
	strcpy(buf,info);
	strcat(buf,"\x1a");//ctrl+z 的ASC 值26
	strcat(cmgs,number);

	send(cmgf,cmgs,buf);
#if 1
	printf("send message success\n");
#endif
	return 0;
}


/*
 *cmgf 选择短信reveive格式
 *
 *cmgs 发送短信
 */

int receive(char *message)//recieve函数，用于AT指令的发送
{
	int nread,nwrite;
	char buff[128];
	char reply[4096];

	if(fd < 0)
	{
		DDEBUG(P_URGENCY,"in the receive, gprs fd check bad.\n");
		return fd;
	}

	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT\r");
	nwrite = write(fd,buff,strlen(buff));
	printf("1_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("1_nread = %d, %s\n", nread, reply);

//////////////////////////////////////////////////////////
/*方式*/
	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT+CMGF=1\r");
	nwrite = write(fd,buff,strlen(buff));
	printf("2_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("2_nread = %d, %s\n", nread, reply);

/********************************************/
	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT+CSCS=\"GSM\"\r");
	nwrite = write(fd,buff,strlen(buff));
	printf("3_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("3_nread = %d, %s\n", nread, reply);
/********************************************/
	/*CNMI*/
	memset(buff,0,sizeof(buff));
	strcpy(buff,"AT+CNMI=2,1,0,0\r");
	nwrite = write(fd,buff,strlen(buff));
	printf("4_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("4_nread = %d, %s\n", nread, reply);
	
	/*CMGR*/
	memset(buff,0,sizeof(buff));
	//strcpy(buff,"AT+CMGR=3\r");
	//strcpy(buff,"AT+CMGL=\"ALL\"\r");
	strcpy(buff,"AT+CMGL=\"REC UNREAD\"\r");	

	nwrite = write(fd,buff,strlen(buff));
	printf("4_nwrite = %d, %s\n", nwrite, buff);

	memset(reply,0,sizeof(reply));
	sleep(1);
	nread = read(fd,reply,sizeof(reply));
	printf("4_nread = %d, %s\n", nread, reply);

	/**/
	strcpy(message,reply);
	printf("call recieve over\n");	
	return 0;
}
int receiver_en_message(char *message)//处理函数，处理字符串
{
	if(fd < 0)
	{
		DDEBUG(P_URGENCY,"in the control, gprs fd check bad.\n");
		return fd;
	}
	char buf[4096];
	receive(buf);
	//strcpy(message,buf);
	getMessage(buf,message);
	

	
#if 1
	printf("receive message success\n");
#endif
	return 0;
}
/*
 *gprs_ctl 控制
 *cmd 为命令 
 *
 *1：SEND MESSAGE
 *2：CALL TELEPHONE 
 *10:温度过高
 *11:湿度过高
 *12:光照过强
 *13：陌生人员进入
 *
 * 成功返回 0 ； 错误返回 -1
 * */
int gprs_ctl(int  cmd, char *message,char *phone)
{	
	char *number ;
	if(fd < 0)
	{
		DDEBUG(P_URGENCY,"in the control, gprs fd check bad.\n");
		return fd;
	}
	
	if(phone == NULL)
		number = "\"13391815013\"";
	else
		number = phone;

	switch (cmd)
	{		
		case GPRS_TEM:
			send_en_message("the temprature is too high",number);break;
		case GPRS_HUM:
			send_en_message("the humidity is too high",number);break;
		case GPRS_ILLU:
			send_en_message("the illumination is too high",number);break;
		case GPRS_STRANGER:
			send_en_message("stranger has come in",number);break;

		case GPRS_ALLMESSAGE:
			receiver_en_message(message);break;
		default :;
	}

	return 0;
}
